﻿#include "cameradahua.h"

static void onGetFrame(IMV_Frame* pFrame, void* pUser){
    if (pFrame == NULL){
        printf("pFrame is NULL\n");
        return;
    }
    //printf("Get frame blockId = %llu\n", pFrame->frameInfo.blockId);

    auto pCamera = static_cast<Camera*>(pUser);
    if(pCamera==nullptr)
        return;
    ImageInfo imgInfo;
    imgInfo.nWidth = pFrame->frameInfo.width;
    imgInfo.nHeight = pFrame->frameInfo.height;
    imgInfo.pData = pFrame->pData;
    imgInfo.nDataLen = pFrame->frameInfo.size;
    imgInfo.enPixelType = pFrame->frameInfo.pixelFormat;
    pCamera->preProcessImage(imgInfo);
}

CameraDahua::CameraDahua(const std::string name):Camera(name)
{

}

void CameraDahua::initialize()
{
    QMutexLocker mutexLocker(&m_lock);
    setStatus(Unavailable);

    // 发现设备
    // discover camera
    IMV_DeviceList deviceInfoList;
    auto ret = IMV_EnumDevices(&deviceInfoList, interfaceTypeAll);
    if (IMV_OK != ret)    {
        printf("Enumeration devices failed! ErrorCode[%d]\n", ret);
        return ;
    }
    int nIndex = -1;
    for (unsigned int i = 0; i < deviceInfoList.nDevNum; i++){
       printf("[device %d]:\n", i);
       auto pDeviceInfo = deviceInfoList.pDevInfo[i];
       std::string cameraName = std::string(reinterpret_cast<char*>(pDeviceInfo.cameraName));
       if(cameraName==m_name){
           nIndex=i;
           break;
       }

    }

    if (nIndex == -1){
        printf("%s unavailable:\n", m_name.c_str());
        return ;
    }

     ret = IMV_CreateHandle(&m_handle, modeByDeviceUserID , (void*)m_name.c_str());
     if (IMV_OK != ret){
         printf("Create devHandle failed! ErrorCode[%d]\n", ret);
         return;
     }

     // 注册数据帧回调函数
     // Register data frame callback function
     ret = IMV_AttachGrabbing(m_handle, onGetFrame, this);
     if (IMV_OK != ret){
         printf("Attach grabbing failed! ErrorCode[%d]\n", ret);
         return;
     }

     setStatus(Closed);
     emitMessage(u8"<font color=\'yellow\'>相机初始化</font>");
}

void CameraDahua::openCamera()
{
    if(m_status != Closed)
        return;

    // Open camera
    auto ret = IMV_Open(m_handle);
    if (IMV_OK != ret){
        printf("Open camera failed! ErrorCode[%d]\n", ret);
        return;
    }

    setStatus(Opened);
    // printf("Open Device succeed! nRet [0x%x]\n", nRet);
    emitMessage(u8"<font color=\'yellow\'>相机打开</font>");

//    unsigned int nSupportedNum;
//    IMV_GetEnumFeatureEntryNum(m_handle,"PixelFormat",&nSupportedNum);
//    IMV_EnumEntryList nSupportValue;
//    nSupportValue.pEnumEntryInfo = new IMV_EnumEntryInfo[nSupportedNum];
//    IMV_GetEnumFeatureEntrys(m_handle,"PixelFormat",&nSupportValue);
//     for(unsigned int i=0;i<nSupportedNum;++i){
//         printf("++++++++++%s\n",nSupportValue.pEnumEntryInfo[i].name);
//         if(IMV_EPixelType::gvspPixelBayBG8 == (IMV_EPixelType)nSupportValue.pEnumEntryInfo[i].value
//                ||IMV_EPixelType::gvspPixelBayGB8 == nSupportValue.pEnumEntryInfo[i].value
//                ||IMV_EPixelType::gvspPixelBayGR8 == nSupportValue.pEnumEntryInfo[i].value
//                ||IMV_EPixelType::gvspPixelBayRG8 == nSupportValue.pEnumEntryInfo[i].value){
//             ret = IMV_SetEnumFeatureValue(m_handle,"PixelFormat",nSupportValue.pEnumEntryInfo[i].value);
//             if(IMV_OK!=ret){
//                 printf("Set Pixel Format fail! nRet [%d]\n", ret);
//             }
//             break;
//         }
//     }
//     delete [] nSupportValue.pEnumEntryInfo;
}

void CameraDahua::startGrabbing()
{
    if(IMV_IsOpen(m_handle)){
        // Start grabbing
        int ret = IMV_StartGrabbing(m_handle);
        if (IMV_OK != ret){
            printf("Start grabbing failed! ErrorCode[%d]\n", ret);
            return;
        }

        setStatus(Grabbing);
        emitMessage(u8"<font color=\'green\'>相机采集</font>");
    }
}

void CameraDahua::stopGrabbing()
{
    if(IMV_IsGrabbing(m_handle)){
        // Stop grabbing
        auto ret = IMV_StopGrabbing(m_handle);
        if (IMV_OK != ret){
            printf("Stop grabbing failed! ErrorCode[%d]\n", ret);
            return;
        }
        emitMessage(u8"相机打开");
        setStatus(Opened);
    }
}

void CameraDahua::closeCamera()
{
    if(IMV_IsOpen(m_handle)){
        // 关闭相机
        // Close camera
        auto ret = IMV_Close(m_handle);
        if (IMV_OK != ret){
            printf("Close camera failed! ErrorCode[%d]\n", ret);
            return;
        }
        emitMessage(u8"相机关闭");
        setStatus(Closed);
    }
}

void CameraDahua::releaseCamera()
{
    // Destroy handle
    if(m_handle){
        auto nRet = IMV_DestroyHandle(m_handle);
        if (IMV_OK != nRet){
            printf("Destroy handle failed! ErrorCode[%d]\n", nRet);
            return;
        }
    }
    m_handle = nullptr;
    setStatus(Unavailable);
}

void CameraDahua::readCameraBrightness(const std::string& camName)
{
    if(camName != m_name)
        return;

    double expCurValue;
    auto nRet = IMV_GetDoubleFeatureValue(m_handle, "ExposureTime",&expCurValue);
    if (IMV_OK != nRet){
        printf("%s GetExposureTime value fail! ErrorCode:[%d]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" MV_CC_GetExposureTime fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
        return;
    }

    double expMax;
    nRet = IMV_GetDoubleFeatureMax(m_handle, "ExposureTime",&expMax);
    if (IMV_OK != nRet){
        printf("%s GetExposureTime max fail! ErrorCode:[0x%x]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" MV_CC_GetExposureTime fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
        return;
    }

    double expMin;
    nRet = IMV_GetDoubleFeatureMin(m_handle, "ExposureTime",&expMin);
    if (IMV_OK != nRet){
        printf("%s GetExposureTime min fail! ErrorCode:[0x%x]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" MV_CC_GetExposureTime fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
        return;
    }

    CameraBrightness cb;
    cb.exp.fCurValue = expCurValue;
    cb.exp.fMax = expMax;
    cb.exp.fMin = expMin;
    emit sigCameraBrightness(m_name, cb);

//    CameraParams cp;
//    cp.exp.fCurValue = expCurValue;
//    cp.exp.fMax = expMax;
//    cp.exp.fMin = expMin;
//    emit sendCameraParams(m_name,cp);
}

void CameraDahua::readCameraWhiteBalance(const std::string& camName)
{
    if(camName != m_name)
        return;

    ///////////////////////////////////white balance red////////////////////
    IMV_SetEnumFeatureSymbol(m_handle,"BalanceRatioSelector","Red");
    double wb_redCurValue;
    auto nRet = IMV_GetDoubleFeatureValue(m_handle, "BalanceRatio",&wb_redCurValue);
    if (IMV_OK != nRet){
        printf("%s Red fail! ErrorCode:[%d]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" MV_CC_GetExposureTime fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
        return;
    }
    double wb_redMax;
    nRet = IMV_GetDoubleFeatureMax(m_handle, "BalanceRatio",&wb_redMax);
    if (IMV_OK != nRet){
        printf("%s Red fail! ErrorCode:[0x%x]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" MV_CC_GetExposureTime fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
        return;
    }
    double wb_redMin;
    nRet = IMV_GetDoubleFeatureMin(m_handle, "BalanceRatio",&wb_redMin);
    if (IMV_OK != nRet){
        printf("%s Red fail! ErrorCode:[0x%x]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" MV_CC_GetExposureTime fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
        return;
    }
    ///////////////////////////////////white balance green////////////////////
    IMV_SetEnumFeatureSymbol(m_handle,"BalanceRatioSelector","Green");
    double wb_greenCurValue;
    nRet = IMV_GetDoubleFeatureValue(m_handle, "BalanceRatio",&wb_greenCurValue);
    if (IMV_OK != nRet){
        printf("%s Green fail! ErrorCode:[0x%x]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" MV_CC_GetExposureTime fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
        return;
    }
    double wb_greenMax;
    nRet = IMV_GetDoubleFeatureMax(m_handle, "BalanceRatio",&wb_greenMax);
    if (IMV_OK != nRet){
        printf("%s Green fail! ErrorCode:[0x%x]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" MV_CC_GetExposureTime fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
        return;
    }
    double wb_greenMin;
    nRet = IMV_GetDoubleFeatureMin(m_handle, "BalanceRatio",&wb_greenMin);
    if (IMV_OK != nRet){
        printf("%s Green fail! ErrorCode:[0x%x]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" MV_CC_GetExposureTime fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
        return;
    }
    ///////////////////////////////////white balance blue////////////////////
    IMV_SetEnumFeatureSymbol(m_handle,"BalanceRatioSelector","Blue");
    double wb_blueCurValue;
    nRet = IMV_GetDoubleFeatureValue(m_handle, "BalanceRatio",&wb_blueCurValue);
    if (IMV_OK != nRet){
        printf("%s Blue fail! ErrorCode:[0x%x]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" MV_CC_GetExposureTime fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
        return;
    }
    double wb_blueMax;
    nRet = IMV_GetDoubleFeatureMax(m_handle, "BalanceRatio",&wb_blueMax);
    if (IMV_OK != nRet){
        printf("%s Blue fail! ErrorCode:[0x%x]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" MV_CC_GetExposureTime fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
        return;
    }
    double wb_blueMin;
    nRet = IMV_GetDoubleFeatureMin(m_handle, "BalanceRatio",&wb_blueMin);
    if (IMV_OK != nRet){
        printf("%s Blue fail! ErrorCode:[0x%x]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" MV_CC_GetExposureTime fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
        return;
    }

    CameraWhiteBalance wb;

    wb.wb_r.nCurValue = wb_redCurValue;
    wb.wb_r.nMax = wb_redMax;
    wb.wb_r.nMin = wb_redMin;
    wb.wb_r.nInc = 1;

    wb.wb_g.nCurValue = wb_greenCurValue;
    wb.wb_g.nMax = wb_greenMax;
    wb.wb_g.nMin = wb_greenMin;
    wb.wb_g.nInc = 1;

    wb.wb_b.nCurValue = wb_blueCurValue;
    wb.wb_b.nMax = wb_blueMax;
    wb.wb_b.nMin = wb_blueMin;
    wb.wb_b.nInc = 1;
    emit sigCameraWhiteBalance(m_name,wb);
}

void CameraDahua::readCameraROI(const std::string& camName)
{
    if(camName != m_name)
        return;

    ////////////////////////////offset x//////////////////
    IMV_SetEnumFeatureSymbol(m_handle,"RegionSelector","Region0");
    int64_t off_xCurValue;
    auto nRet = IMV_GetIntFeatureValue(m_handle, "OffsetX",&off_xCurValue);
    if (IMV_OK != nRet){
        printf("%s Red fail! ErrorCode:[0x%x]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" MV_CC_GetExposureTime fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
        return;
    }
    int64_t off_xMax;
    nRet = IMV_GetIntFeatureMax(m_handle, "OffsetX",&off_xMax);
    if (IMV_OK != nRet){
        printf("%s Red fail! ErrorCode:[0x%x]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" MV_CC_GetExposureTime fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
        return;
    }
    int64_t off_xMin;
    nRet = IMV_GetIntFeatureMin(m_handle, "OffsetX",&off_xMin);
    if (IMV_OK != nRet){
        printf("%s Red fail! ErrorCode:[0x%x]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" MV_CC_GetExposureTime fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
        return;
    }
    int64_t off_xInc;
    nRet = IMV_GetIntFeatureInc(m_handle, "OffsetX",&off_xInc);
    if (IMV_OK != nRet){
        printf("%s Red fail! ErrorCode:[0x%x]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" MV_CC_GetExposureTime fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
        return;
    }
    ////////////////////////////offset y//////////////////
    IMV_SetEnumFeatureSymbol(m_handle,"RegionSelector","Region0");
    int64_t off_yCurValue;
    nRet = IMV_GetIntFeatureValue(m_handle, "OffsetY",&off_yCurValue);
    if (IMV_OK != nRet){
        printf("%s Red fail! ErrorCode:[0x%x]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" MV_CC_GetExposureTime fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
        return;
    }
    int64_t off_yMax;
    nRet = IMV_GetIntFeatureMax(m_handle, "OffsetY",&off_yMax);
    if (IMV_OK != nRet){
        printf("%s Red fail! ErrorCode:[0x%x]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" MV_CC_GetExposureTime fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
        return;
    }
    int64_t off_yMin;
    nRet = IMV_GetIntFeatureMin(m_handle, "OffsetY",&off_yMin);
    if (IMV_OK != nRet){
        printf("%s Red fail! ErrorCode:[0x%x]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" MV_CC_GetExposureTime fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
        return;
    }
    int64_t off_yInc;
    nRet = IMV_GetIntFeatureInc(m_handle, "OffsetY",&off_yInc);
    if (IMV_OK != nRet){
        printf("%s Red fail! ErrorCode:[0x%x]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" MV_CC_GetExposureTime fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
        return;
    }

    CameraROI roi;
    roi.off_x.nMin = off_xMin;
    roi.off_x.nMax = off_xMax;
    roi.off_x.nInc = off_xInc;
    roi.off_x.nCurValue = off_xCurValue;

    roi.off_y.nMin = off_yMin;
    roi.off_y.nMax = off_yMax;
    roi.off_y.nInc = off_yInc;
    roi.off_y.nCurValue = off_yCurValue;
    emit sigCameraROI(m_name,roi);
}

void CameraDahua::setCameraROI(const std::string& camName, const CameraROI &roi)
{
    if(camName!=m_name)
        return;
    int nRet = IMV_OK;

    nRet = IMV_SetIntFeatureValue(m_handle,"OffsetX",roi.off_x.nCurValue);
    if (IMV_OK != nRet){
        printf("%s SetAOIoffsetX fail! ErrorCode:[%d]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" MV_CC_SetAOIoffsetX fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
    }
    nRet = IMV_SetIntFeatureValue(m_handle,"OffsetY",roi.off_y.nCurValue);
    if (IMV_OK != nRet){
        printf("%s SetAOIoffsetY fail! ErrorCode:[%d]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" MV_CC_SetAOIoffsetY fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
    }

    //save params to user set
    nRet = IMV_SetEnumFeatureSymbol(m_handle, "UserSetSelector", "UserSet1");
    if (IMV_OK != nRet){
        printf("%s SetUserSetSelector fail! ErrorCode:[%d]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" MV_CC_SetUserSetSelector fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
    }

    nRet =IMV_ExecuteCommandFeature(m_handle,"UserSetSave");
    if (IMV_OK != nRet){
        printf("%s SetUserSerSave fail! ErrorCode:[%d]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" MV_CC_SetUserSerSave fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
    }
}

void CameraDahua::setCameraWhiteBalance(const std::string& camName, const CameraWhiteBalance &cw)
{
    if(camName!=m_name)
        return;
    int nRet = IMV_OK;

    nRet = IMV_SetEnumFeatureSymbol(m_handle,"BalanceRatioSelector","Red");
    nRet = IMV_SetDoubleFeatureValue(m_handle,"BalanceRatio",cw.wb_r.nCurValue);
    if (IMV_OK != nRet){
        printf("%s MV_CC_SetBalanceRatioRed fail! ErrorCode:[0x%x]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" SetBalanceRatioRed fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
    }
    nRet = IMV_SetEnumFeatureSymbol(m_handle,"BalanceRatioSelector","Green");
    nRet = IMV_SetDoubleFeatureValue(m_handle,"BalanceRatio",cw.wb_g.nCurValue);
    if (IMV_OK != nRet){
        printf("%s MV_CC_SetBalanceRatioGreen fail! ErrorCode:[0x%x]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" SetBalanceRatioGreen fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
    }
    nRet = IMV_SetEnumFeatureSymbol(m_handle,"BalanceRatioSelector","Blue");
    nRet =IMV_SetDoubleFeatureValue(m_handle,"BalanceRatio",cw.wb_b.nCurValue);
    if (IMV_OK != nRet){
        printf("%s MV_CC_SetBalanceRatioBlue fail! ErrorCode:[0x%x]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" SetBalanceRatioBlue fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
    }

    //save params to user set
    nRet = IMV_SetEnumFeatureSymbol(m_handle, "UserSetSelector", "UserSet1");
    if (IMV_OK != nRet){
        printf("%s SetUserSetSelector fail! ErrorCode:[%d]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" MV_CC_SetUserSetSelector fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
    }

    nRet =IMV_ExecuteCommandFeature(m_handle,"UserSetSave");
    if (IMV_OK != nRet){
        printf("%s SetUserSerSave fail! ErrorCode:[%d]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" MV_CC_SetUserSerSave fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
    }
}

void CameraDahua::setCameraBrightness(const std::string& camName,  const CameraBrightness &cb)
{
    if(camName!=m_name)
        return;
    int nRet = IMV_OK;

    nRet =IMV_SetDoubleFeatureValue(m_handle,"ExposureTime", cb.exp.fCurValue);
    if (IMV_OK != nRet){
        printf("%s SetExposureTime fail! ErrorCode:[0x%x]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" MV_CC_SetExposureTime fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
    }

    //save params to user set
    nRet = IMV_SetEnumFeatureSymbol(m_handle, "UserSetSelector", "UserSet1");
    if (IMV_OK != nRet){
        printf("%s SetUserSetSelector fail! ErrorCode:[%d]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" MV_CC_SetUserSetSelector fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
    }

    nRet =IMV_ExecuteCommandFeature(m_handle,"UserSetSave");
    if (IMV_OK != nRet){
        printf("%s SetUserSerSave fail! ErrorCode:[%d]\n",m_name.c_str(), nRet);
        //LOG(WARNING)<<m_camName<<" MV_CC_SetUserSerSave fail! ErrorCode: "<< QByteArray::number(nRet,16).toStdString();
    }
}

void CameraDahua::preProcessImage(const ImageInfo &imgInfo)
{
    if(imgInfo.pData==nullptr){
        return;
    }

    IMV_EPixelType enDstPixelType = gvspPixelTypeUndefined;
    unsigned int nChannelNum = 0;
    QImage::Format imgFormat;
    int cvFormat = CV_8UC3;
    if (isColor((IMV_EPixelType)imgInfo.enPixelType)){
        nChannelNum = 3;
        enDstPixelType = (IMV_EPixelType)imgInfo.enPixelType==gvspPixelRGB8?enDstPixelType:gvspPixelRGB8;
        imgFormat = QImage::Format_RGB888;
        cvFormat = CV_8UC3;
    }
    else if (isMono((IMV_EPixelType)imgInfo.enPixelType)){
        nChannelNum = 1;
        enDstPixelType = (IMV_EPixelType)imgInfo.enPixelType==gvspPixelMono8?enDstPixelType:gvspPixelMono8;
        imgFormat = QImage::Format_Mono;
        cvFormat = CV_8UC1;
    }

    if (enDstPixelType != gvspPixelTypeUndefined){
        unsigned int nConvertDataSize = 0;
        unsigned char *pConvertData=NULL;
        nConvertDataSize = imgInfo.nWidth * imgInfo.nHeight * nChannelNum;
        pConvertData = (unsigned char*)malloc(nConvertDataSize);
        if (NULL == pConvertData){
            printf("malloc pConvertData fail!\n");
            return;
        }

        // Convert pixel format
        IMV_PixelConvertParam  stConvertParam = {0};

        stConvertParam.nWidth = imgInfo.nWidth;                 // image width
        stConvertParam.nHeight = imgInfo.nHeight;               // image height
        stConvertParam.pSrcData = imgInfo.pData;                         // input data buffer
        stConvertParam.nSrcDataLen = imgInfo.nDataLen;         // input data size
        stConvertParam.ePixelFormat = (IMV_EPixelType)imgInfo.enPixelType;    // input pixel format
        stConvertParam.eBayerDemosaic = IMV_EBayerDemosaic::demosaicEdgeSensing; //input bayer method
        stConvertParam.eDstPixelFormat = enDstPixelType;         // output pixel format
        stConvertParam.pDstBuf = pConvertData;                               // output data buffer
        stConvertParam.nDstBufSize = nConvertDataSize;                       // output buffer size
        auto nRet = IMV_PixelConvert(m_handle, &stConvertParam);
        if (IMV_OK != nRet){
            printf("Convert Pixel Type fail! nRet [%d]\n", nRet);
            //return;
        }

//        QImage image((uchar*)pConvertData,imgInfo.nWidth,imgInfo.nHeight,imgFormat);
//        emit sendImage(m_name, image.copy());

        cv::Mat cv_img(imgInfo.nHeight,imgInfo.nWidth,cvFormat,(uchar*)pConvertData);
        cv::cvtColor(cv_img,cv_img,cv::COLOR_RGB2BGR);
        emit sendCVImage(m_name, cv_img.clone());
        free(pConvertData);
        pConvertData = NULL;
    }else{
//        unsigned char *pData = NULL;
//        unsigned int nDataSize = imgInfo.nWidth * imgInfo.nHeight * nChannelNum;
//        pData = (unsigned char*)malloc(nDataSize);
//        if (NULL == pData){
//            printf("malloc pConvertData fail!\n");
//            return;
//        }
//        memcpy(pData,imgInfo.pData,nDataSize);
//        QImage image((uchar*)pData,imgInfo.nWidth,imgInfo.nHeight,imgFormat);
//        emit sendImage(m_name, image.copy());
//        free(pData);
//        pData = NULL;

        cv::Mat cv_img(imgInfo.nHeight,imgInfo.nWidth,cvFormat,imgInfo.pData);
        cv::cvtColor(cv_img,cv_img,cv::COLOR_RGB2BGR);
        emit sendCVImage(m_name, cv_img.clone());
    }
}


bool CameraDahua::isColor(int pixelFormat)
{
    switch((IMV_EPixelType)pixelFormat){
    case gvspPixelRGB8:
    case gvspPixelBGR8:
    case gvspPixelYUV422_8:
    case gvspPixelYUV422_8_UYVY:
    case gvspPixelBayGB8:
    case gvspPixelBayBG8:
    case gvspPixelBayRG8:
    case gvspPixelBayGR8:
    case gvspPixelBayGB10:
    case gvspPixelBayBG10:
    case gvspPixelBayRG10:
    case gvspPixelBayGR10:
    case gvspPixelBayGB12:
    case gvspPixelBayBG12:
    case gvspPixelBayRG12:
    case gvspPixelBayGR12:
       return true;
    default:
       return false;
    }
}

bool CameraDahua::isMono(int pixelFormat)
{
    switch((IMV_EPixelType)pixelFormat){
    case gvspPixelMono8:
    case gvspPixelMono10:
    case gvspPixelMono12:
        return true;
    default:
        return false;
    }
}
